﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Text;
using CookComputing.XmlRpc;
using Microsoft.SharePoint.Security;
using VH.SharePoint.Api.Blogger;
using VH.SharePoint.Api.MetaWeblog;
using VH.SharePoint.Api.Queries;

namespace VH.SharePoint.Api
{
    [XmlRpcService(AutoDocumentation = true, Name = "SharePoint Blog API", Description = "The SharePoint Blog Api provide access to the Publishing features from any Weblog clients.")]
    public class BlogApi : XmlRpcService, IBlogApi
    {
        #region Repository
        public BlogRepository BlogRepository
        {
            get { return Storage.BlogRepository; }
        }
        #endregion

        #region MetaWeblog Implementation

        string IMetaWebLog.NewPost(string blogId, string userName, string password, Post content, bool publish)
        {
            BlogRepository.Posts.ValidateUser(userName, password);
            BlogRepository.Posts.Create(content);
            BlogRepository.Posts.Publish(content, publish);
            return content.Id.ToString(CultureInfo.InvariantCulture);
        }

        bool IMetaWebLog.EditPost(object postId, string userName, string password, Post content, bool publish)
        {
            BlogRepository.Posts.ValidateUser(userName, password);
            content.Id = int.Parse(postId.ToString());
            BlogRepository.Posts.Update(content);
            BlogRepository.Posts.Publish(content, publish);
            return true;
        }

        Post IMetaWebLog.GetPost(object postId, string username, string password)
        {
            BlogRepository.Posts.ValidateUser(username, password);
            return BlogRepository.Posts.Get(postId.ToString());
        }

        Post[] IMetaWebLog.GetRecentPosts(string blogId, string username, string password, int numberOfPosts)
        {
            BlogRepository.Posts.ValidateUser(username, password);
            if (numberOfPosts <= 0)
            {
                return BlogRepository.Posts.FindAll().ToArray();
            }
            var constraints = new QueryConstraints<Post>{ PageSize = numberOfPosts};
            return BlogRepository.Posts.FindAll(constraints).ToArray();
        }

        bool IMetaWebLog.DeletePost(object postId, string username, string password)
        {
            BlogRepository.Posts.ValidateUser(username, password);
            var post = BlogRepository.Posts.Get(postId.ToString());
            BlogRepository.Posts.Delete(post);
            return true;
        }

        MediaObjectInfo IMetaWebLog.NewMediaObject(string blogId, string username, string password, MediaObject mediaInfo)
        {
            BlogRepository.Medias.ValidateUser(username, password);
            if (!mediaInfo.Overwrite)
            {
                try
                {
                    return BlogRepository.Medias.Get(mediaInfo.FileName);
                }
                catch (Exception) // Not Found
                {

                }
            }
            var res = new MediaObjectInfo {FileName = mediaInfo.FileName, MimeType = mediaInfo.MimeType};
            BlogRepository.Medias.Create(res, mediaInfo.Media);
            return res;
        }

        Category[] IMetaWebLog.GetCategories(string blogId, string username, string password)
        {
            BlogRepository.Categories.ValidateUser(username, password);
            return BlogRepository.Categories.FindAll().ToArray();
        }

        #endregion

        #region Blogger Implementation

        Blog[] IBlogger.GetUsersBlogs(string appKey, string username, string password)
        {
            BlogRepository.ValidateUser(username, password);
            return BlogRepository.FindAll().ToArray();
        }

        bool IBlogger.DeletePost(string appKey, object postId, string username, string password, bool publish)
        {
            BlogRepository.Posts.ValidateUser(username, password);
            var post = BlogRepository.Posts.Get(postId.ToString());
            BlogRepository.Posts.Delete(post);
            return true;
        }

        #endregion

        #region MovableType Implementation

        bool MovableType.IMovableType.SetCategories(object postId, string username, string password, Category[] categories)
        {
            //Todo:Unsupported yet
            return true;
        }

        #endregion

        #region SharePoint Handler Options

        public Boolean AllowAnonymousAccess {
            get { return true; }
        }

        public Boolean AllowNullWeb {
            get { return true; }
        }
        
        #endregion

       
    }
}
